﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace livingwellclinic
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SortedList monetaryinfo = new SortedList();
        SortedList suppliesinfo = new SortedList();
        string paymentstorage;
        string amountstorage;
        string cardnumberstorage;
        string suppliesstorage;
        protected void Page_Load(object sender, EventArgs e)
        {
            //submitbutton.Visible = false;
        }

        protected void cancelbutton_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx");
            suppliesRadio.Checked = false;
            monetaryRadio.Checked = false;
        }

        protected void CardNumberCustomValidator_ServerValidate(object sourse, ServerValidateEventArgs args)
        {
            args.IsValid = ValidateCreditCard(args.Value);
        }

        private bool ValidateCreditCard(string cardNumber)
        {
            int digitsSum = 0;
            string digits = "";
            string reversedCardNumber = "";
            //Remove spaces and reverse string
            cardNumber = cardNumber.Replace(" ", null);
            for (int i = 0; i < reversedCardNumber.Length; i++)
            {
                if ((i + 1) % 2 == 0)
                {
                    digits += Convert.ToInt32(reversedCardNumber.Substring(i, 1)) * 2;
                }
                else
                    digits += reversedCardNumber.Substring(i, 1);
            }
            //add the digits
            for (int i = 0; i < digits.Length; i++)
                digitsSum += Convert.ToInt32(digits.Substring(i, 1));
            //Check that the sum is divisible by 10
            if ((digitsSum % 10) == 0)
                return true;
            else
                return false;
        }

        protected void submitbutton_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex == 0)
            {
                ddlvalid.IsValid = false;
                if (monetaryRadio.Checked == true)
                {
                    paymentstorage = Convert.ToString(paymentType.SelectedValue);
                    amountstorage = amounttxt.Text;
                    cardnumberstorage = cardnumbertxt.Text;
                    monetaryinfo.Add("paymentmethod", paymentstorage);
                    monetaryinfo.Add("amount", amountstorage);
                    monetaryinfo.Add("cardnumber", cardnumberstorage);
                    Session.Add("DonateSession", monetaryinfo);
                    //elbl.Text = (string)monetaryinfo.GetByIndex(0);
                    //elbl.Text = (string)monetaryinfo.GetByIndex(1);
                }
                if (suppliesRadio.Checked == true)
                {
                    suppliesstorage = suppliestxt.Text;
                    suppliesinfo.Add("supplies", suppliesstorage);
                    Session.Add("DonateSession", suppliesinfo);
                    //slbl.Text = (string)suppliesinfo.GetByIndex(0);
                }
            }
            else
                ddlvalid.IsValid = true;
        }

        protected void showSubmit(object sender, EventArgs e)
        {
            //submitbutton.Visible = true;
        }
    }
}